package com.ruoyi.web.controller.publish;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.publish.domain.IdExpert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.publish.domain.IdPublish;
import com.ruoyi.publish.service.IIdPublishService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 出版单位管理Controller
 *
 * @author 张泽望
 * @date 2020-11-03
 */
@Controller
@RequestMapping("/publish/unit")
public class IdPublishController extends BaseController {
    private String prefix = "publish/unit";

    @Autowired
    private IIdPublishService idPublishService;

    //    @RequiresPermissions("publish:unit:view")
    @GetMapping()
    public String unit() {
        return prefix + "/unit";
    }

    /**
     * 查询出版单位管理列表
     */
//    @RequiresPermissions("publish:unit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IdPublish idPublish) {
        startPage();
        List<IdPublish> list = idPublishService.selectIdPublishList(idPublish);
        return getDataTable(list);
    }

    /**
     * 导出出版单位管理列表
     */
//    @RequiresPermissions("publish:unit:export")
    @Log(title = "出版单位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IdPublish idPublish) {
        List<IdPublish> list = null;
        if (StringUtils.isNotEmpty(idPublish.getExportIds())) {
            list = idPublishService.selectRecordByIds(idPublish.getExportIds().split(","));
        } else {
            list = idPublishService.selectIdPublishList(idPublish);
        }
        ExcelUtil<IdPublish> util = new ExcelUtil<IdPublish>(IdPublish.class);
        return util.exportExcel(list, "unit");
    }

    /**
     * 新增出版单位管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存出版单位管理
     */
//    @RequiresPermissions("publish:unit:add")
    @Log(title = "出版单位管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IdPublish idPublish) {
        idPublish.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(idPublishService.insertIdPublish(idPublish));
    }

    /**
     * 修改出版单位管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        IdPublish idPublish = idPublishService.selectIdPublishById(id);
        mmap.put("idPublish", idPublish);
        return prefix + "/edit";
    }

    /**
     * 查看出版单位管理
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        IdPublish idPublish = idPublishService.selectIdPublishById(id);
        mmap.put("idPublish", idPublish);
        return prefix + "/detail";
    }

    /**
     * 修改保存出版单位管理
     */
//    @RequiresPermissions("publish:unit:edit")
    @Log(title = "出版单位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IdPublish idPublish) {
        return toAjax(idPublishService.updateIdPublish(idPublish));
    }

    /**
     * 删除出版单位管理
     */
//    @RequiresPermissions("publish:unit:remove")
    @Log(title = "出版单位管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(idPublishService.deleteIdPublishByIds(ids));
    }

    /**
     * 获取数据集合
     */
    @RequestMapping("/publishCollection")
    @ResponseBody
    public TableDataInfo collection(IdPublish idPublish) {
        List<IdPublish> list;
        String companyName = idPublish.getCompanyName();

        if (companyName != null && StringUtils.isNotEmpty(companyName)) {
            startPage();
            list = idPublishService.selectIdPublishList(idPublish);
        } else {
            list = new ArrayList<IdPublish>();
        }
//        AjaxResult ajax = new AjaxResult();
//        ajax.put("items", list);
        return getDataTable(list);
    }

    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {

        ExcelUtil<IdPublish> util = new ExcelUtil<IdPublish>(IdPublish.class);
        List<IdPublish> list = new ArrayList<>();
        IdPublish idPublish = new IdPublish();
        //单位名称
        idPublish.setCompanyName("单位名称(示范)");
        //主管单位
        idPublish.setCompetentUnit("主管单位(示范)");
        //主办单位
        idPublish.setOrganizers("主办单位(示范)");
        //总编姓名
        idPublish.setEditorChief("总编(示范)");
        //联系电话
        idPublish.setTelephone("12345678(示范)");
        //电子邮件
        idPublish.setEmail("12345678@qq.com(示范)");
        //审读联系人
        idPublish.setReviewer("张三(示范)");
        //单位地址
        idPublish.setAddress("单位地址(示范)");
        //其他说明
        idPublish.setInstructions("说明(示范)");
        //单位类型
        idPublish.setBy1("报社(示范)");
        list.add(idPublish);
        return util.importDataTemplateExcel(list, "出版单位数据");
    }

    @Log(title = "出版单位excel导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport, String year) throws Exception {
        byte[] bytes = file.getBytes();
        InputStream in = new ByteArrayInputStream(bytes);

        ExcelUtil<IdPublish> util = new ExcelUtil<IdPublish>(IdPublish.class);
        List<IdPublish> userList = util.importExcel(in);

        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = idPublishService.importIdPublish(userList, updateSupport, operName, year);
        return AjaxResult.success(message);
    }
}
